<?php

namespace app\index\controller;

use app\index\model\Banner;
use app\mall\model\MallDesigners;
use app\mall\model\MallGoods;
use app\mall\model\MallGoodsCate;
use app\mall\model\MallGoodsMark;
use app\mall\model\MallQuestion;
use app\mall\model\MallUserActionCollect;
use app\mall\service\ConfigService;
use think\admin\Controller;

class Index extends Base
{
    public function index()
    {

        $this->banner_list = Banner::order('sort', 'desc')->select();
        $marks = MallGoodsMark::items_list();
        foreach ($marks as $key => $value) {
            $marks[$key]['goods'] = MallGoods::where('marks',  $value['id'])->limit(2)->field('id,name,cover_slide,code')->select()->toArray();
        }
        $this->marks_goods = $marks;
        $good_list = MallGoods::where('status', 1)->where('deleted',0)->limit(4)->field('id,name,cover,code')->order('sort desc')->select();
        foreach ($good_list as $key => $value) {
            $value->is_collect = MallUserActionCollect::where('unid', $this->uid)->where('gcode', $value->code)->count();
        }
        $this->goods_list = $good_list;
        $this->cat_list   = MallGoodsCate::where('status', 1)->where('deleted',0)->limit(4)->field('id,name,cover')->order('sort desc')->select();
        $this->designer_list = MallDesigners::with(['goods'=>function($query){
            $query->field('id,name,cover,designer_id,code');
        }])->limit(4)->order('sort desc')->select();
        $this->about_us = ConfigService::getPage('about_us');
        return $this->fetch();
    }

    /**
     * 关于我们
     */
    public function company(){
        $this->about_us_detail  = ConfigService::getPage('about_us_detail');
        $this->career           = ConfigService::getPage('career');
        $this->future_plan      = ConfigService::getPage('future_plan');
        $this->collaboration    = ConfigService::getPage('collaboration');
        return $this->fetch();
    }

    //帮助
    public function help(){
        $this->reture_policy       = ConfigService::getPage('reture_policy');
        $this->insurcame_polily    = ConfigService::getPage('insurcame_polily');
        $this->customer_service    = ConfigService::getPage('customer_service');
        $this->config              = ConfigService::get();
        $this->questions           = MallQuestion::order('sort desc')->limit(4)->select();
        return $this->fetch();
    }

    //留言
    public function question(){
        $data = $this->_vali([
            'question.require' => 'Question is Require'
        ]);
        if(MallQuestion::create($data)) $this->success('ok');
        $this->error('error');
    }

}
